package main.java.com.wen.controller;

import com.wen.entity.Pager;
import com.wen.entity.UserCenter;
import com.wen.service.UserCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 001 on 2017/6/12.
 */
@Controller
@RequestMapping("/user")
public class UserCenterController {
    private Map<String,UserCenter> users=new HashMap<String, UserCenter>();



    @RequestMapping(value = "/users",method = RequestMethod.GET)
    public String list(Model model){
        model.addAttribute("users", users);
        return "user/list";
    }

    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(){
        return "user/add";
    }


    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(UserCenter userCenter){
        System.out.print("<<<<<<<<<<<<<<<<<<<"+userCenter.getUsername());
        userCenterService.insert(userCenter);
        return "redirect:/user/showuserlist";
    }


    /**
     * 根据ID删除一个student用户
     * 在students.jsp删除按钮上提交ID，controller使用@PathVariable获取ID
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/delete")
    public String delete(@PathVariable int id){
        userCenterService.deleteByPrimaryKey(id);
        return "redirect:/user/showuserlist";
    }


    @RequestMapping(value = "/{id}/update",method = RequestMethod.GET)
    public String update(@PathVariable int id,Model model){
        userCenterService.selectOneUser(id);
        model.addAttribute("userCenter",userCenterService.selectOneUser(id));
        return "user/update";
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(@Validated UserCenter userCenter){
       userCenterService.updateByPrimaryKey(userCenter);
        return "redirect:/user/showuserlist";
}




    @Autowired
    UserCenterService userCenterService;

    @RequestMapping(value = "/show", method = RequestMethod.GET)
    public String show(Model model) {
        model.addAttribute("user", userCenterService.selectOneUser(1));
        return "user/show";
    }
//    @RequestMapping(value = "/show_all_user", method = RequestMethod.GET)
//    public String show_all_user(Model model) {
//        List<UserCenter> userCenters=userCenterService.findAllUser();
//        model.addAttribute("users",userCenters );
//        return "user/showAllUser";
//    }

    @RequestMapping(value = "/showuserlist", method = RequestMethod.GET)
    public String showuserlist(Model model,HttpServletRequest request) {
        int page = 0;
        if (request.getParameter("pager.offset") != null) {
            page = Integer.parseInt(request.getParameter("pager.offset"));
        }
int allPages;
        Pager<UserCenter> userCenterPager = new Pager<UserCenter>();
        if (userCenterService.allPages() % userCenterPager.getPageSize() == 0) {
            allPages = userCenterService.allPages() / userCenterPager.getPageSize();
        } else allPages = userCenterService.allPages() / userCenterPager.getPageSize() + 1;
        userCenterPager.setTotalPage(allPages);
//        总记录
        userCenterPager.setTotalRecord(userCenterService.allPages());
//        设置当前页
        int thisPage = (page + userCenterPager.getPageSize()) / userCenterPager.getPageSize();
        userCenterPager.setPageIndex(thisPage);
        List<UserCenter> userCenters=userCenterService.selectStudentByPage(page,userCenterPager.getPageSize());
        model.addAttribute("pager",userCenterPager);
        model.addAttribute("users",userCenters );
        return "user/userlist";
    }

}